/**
 * 
 */
package name.marz.to.go.btree.ui.swing;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

/**
 * @author masaru-gotou
 *
 */
public class InitializeControlPanel extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JButton btn;
	private JRadioButton initRandom;
	private JRadioButton initDefault;
	private JRadioButton initSequence;
	private JTextField keyCountInNode;
	private JTextField recordCount;
	private JPanel textPanel;
	
	public InitializeControlPanel() {
		initDefault = new JRadioButton("Init as default");
		initRandom = new JRadioButton("Init as random");
		initSequence = new JRadioButton("Init as Sequencial(TreeMapNode)");
		ButtonGroup group = new ButtonGroup();
		group.add(initDefault);
		group.add(initRandom);
		group.add(initSequence);
		ActionListener radiolistener = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (e.getSource() == initDefault) setTextFieldEnabled(false);
				if (e.getSource() == initRandom) setTextFieldEnabled(true);
				if (e.getSource() == initSequence) setTextFieldEnabled(true);
			}
		};
		
		initDefault.addActionListener(radiolistener);
		initRandom.addActionListener(radiolistener);
		initSequence.addActionListener(radiolistener);
		
		keyCountInNode = new JTextField(3);
		keyCountInNode.setText("2");
		recordCount = new JTextField(3);
		recordCount.setText("10");
		
		btn = new JButton("Init");
		
		textPanel = new JPanel();
		textPanel.add(new JLabel("keys"));
		textPanel.add(keyCountInNode);
		textPanel.add(new JLabel("records"));
		textPanel.add(recordCount);
		
		GridBagLayout layout = new GridBagLayout();
//		BorderLayout layout = new BorderLayout();
		setLayout(layout);
		JPanel radioPanel = new JPanel(new GridLayout(0,1));
		radioPanel.add(initDefault);
		radioPanel.add(initRandom);
		radioPanel.add(initSequence);
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.anchor = GridBagConstraints.WEST;
		gbc.gridx = 0; gbc.gridy = 0; gbc.gridwidth = 1; gbc.gridheight = 1;
		layout.setConstraints(radioPanel, gbc);
		add(radioPanel);
		gbc.gridx = 0; gbc.gridy = 1; gbc.gridwidth = 1; gbc.gridheight = 1;
		layout.setConstraints(textPanel, gbc);
		add(textPanel);
		gbc.gridx = 1; gbc.gridy = 0; gbc.gridwidth = 1; gbc.gridheight = 2;
		layout.setConstraints(btn, gbc);
		add(btn);
		
		initDefault.setSelected(true);
		setTextFieldEnabled(false);
	}
	private void setTextFieldEnabled(boolean b) {
		for ( int i=0; i<textPanel.getComponentCount(); i++ ) {
			textPanel.getComponent(i).setEnabled(b);
		}
	}
	public int getKeyCountInNode() {
		return Integer.parseInt(keyCountInNode.getText());
	}
	public int getRecordCount() {
		return Integer.parseInt(recordCount.getText());
	}
	public void addInitActionListener(ActionListener l) {
		btn.addActionListener(l);
	}
	public boolean isInitDefaultSelected() {
		return initDefault.isSelected();
	}
	public boolean isInitRandomSelected() {
		return initRandom.isSelected();
	}
	public boolean isInitSequenceSelected() {
		return initSequence.isSelected();
	}
}
